<?php
namespace app\admin\controller;
use think\Controller;
use catetree\CateTree;

class Cate extends Controller
{
    public function lst()
    {
        $cateRes = db('Cate')->select();
        $cate = new CateTree();
        $cateRes = $cate->catetree($cateRes);
        $this->assign([
            'cateRes' => $cateRes,
            ]);
        return view('list');
    }

    public function add()
    {
    	if(request()->isPost()){
    		$data = input('post.');
    		// dump($data);die;


            // 验证数据
            // $validate = validate('Brand');
            // if(!$validate->check($data)){
            //     $this->error($validate->getError());
            // }


            $add = db('cate')->insert($data);
            if($add){
                $this->success('添加分类成功！', 'lst');
            }else{
                $this->error('添加分类失败！');
            }
    		return;
    	}

        $cateRes = db('Cate')->select();
        $this->assign([
            'cateRes' => $cateRes,
            ]);
        return view();
    }

    public function edit()
    {
        if(request()->isPost()){
            $data = input('post.');
            

            // // 验证数据
            // $validate = validate('Brand');
            // if(!$validate->check($data)){
            //     $this->error($validate->getError());
            // }

            $save = db('cate')->update($data);
            if($save !== false){
                $this->success('修改分类成功！', 'lst');
            }else{
                $this->error('修改分类失败！');
            }

            return; 
        }

        $id = input('id');
        $brands = db('cate')->find($id);
        $this->assign([
            'brands' => $brands,
            ]);
        return view();
    }

    public function del($id)
    {
        $del = db('cate')->delete($id);
        if($del){
            $this->success('删除分类成功！', 'lst');
        }else{
            $this->error('删除分类失败！');
        }
        return;
    }


}
